/*!
 * Copyright (C) 2020 - All Rights Reserved by
 * @author : ZhaoYanbo
 * @email  : zyb920@hotmail.com
 * @created: 2020-10-31
 * @version: 1.0.0.0
 *
 */

#ifndef LISTMODEL_H
#define LISTMODEL_H

#include <QAbstractListModel>

struct Data {
    bool isHead;
    bool isExpand;
    QString name;
    Data *parent;
    QList<Data *> subs;

    Data(Data *p = nullptr)
    {
        isHead = false;
        isExpand = false;
        this->parent = p;
    }
    ~Data()
    {
        this->parent = nullptr;
        subs.clear();
    }
};
Q_DECLARE_METATYPE(Data)

class ListModel : public QAbstractListModel
{
    Q_OBJECT
public:
    enum ListRoles{
        HeadRole = Qt::UserRole + 1,
        ExpandRole,
        NameRole
    };
    explicit ListModel(QObject *parent = nullptr);
    ~ListModel() override;

    int rowCount(const QModelIndex &parent) const override;
    int columnCount(const QModelIndex &parent) const override;

    QVariant data(const QModelIndex &index, int role) const override;
    bool setData(const QModelIndex &index, const QVariant &value, int role) override;

    Qt::ItemFlags flags(const QModelIndex &index) const override;

    bool isValid(int index) const;// index是否有效

    //
    bool append(Data *d);
    bool append(QString name, bool head = true);
    bool insert(int index, Data *d);

    Data *get(int index);

private:
    QList<Data *> m_data_list;
};

#endif // LISTMODEL_H
